package com.easyCode.dataMock;

import com.easyCode.dataMock.constants.MockRuleConstants;
import com.easyCode.dataMock.dataGenerator.*;
import org.springframework.util.Assert;

import java.util.*;

/**
 * @author 大忽悠
 * @create 2023/1/18 11:22
 */
public class DataMockManager {
    /**
     * 表信息列表
     */
    private final List<TableMockInfo> tableMockInfoList = new ArrayList<>();
    /**
     * 数据生成器
     */
    private static final Map<Integer, DataGenerator> DATA_GENERATOR_MAPPINGS = new HashMap<>();

    static {
        DATA_GENERATOR_MAPPINGS.put(MockRuleConstants.FIXED, new FixedDataGenerator());
        DATA_GENERATOR_MAPPINGS.put(MockRuleConstants.INCREMENT, new IncreaseDataGenerator());
        DATA_GENERATOR_MAPPINGS.put(MockRuleConstants.RULE, new RuleDataGenerator());
        DATA_GENERATOR_MAPPINGS.put(MockRuleConstants.RANDOM, new RandomDataGenerator());
        DATA_GENERATOR_MAPPINGS.put(MockRuleConstants.DICT, new DictDataGenerator());
    }


    public void addMockObjects(Object... obj) {
        tableMockInfoList.addAll(MockMapper.mapper(obj));
    }

    /**
     * 生成模拟数据---生成一堆Insert语句
     */
    public List<String> mockData() {
        List<String> insertList = new ArrayList<>();
        for (TableMockInfo tableMockInfo : tableMockInfoList) {
              insertList.addAll(tableMockInfo.getInsertList());
        }
        return insertList;
    }

    public static DataGenerator getDataGenerator(Integer strategy){
        DataGenerator dataGenerator = DATA_GENERATOR_MAPPINGS.get(strategy);
        Assert.notNull(dataGenerator,"无法通过key为"+strategy+"的映射找到对应的dataGenerator!");
        return dataGenerator;
    }
}
